package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.BlogUserSettings;

public class GenericBlogUserSettingsDAO implements
		org.jetnuke.dao.BlogUserSettingsDAO {

	public BlogUserSettings retrieveByPk(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogUserSettingsModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		BlogUserSettings s = new BlogUserSettings();
		
		if (rs.next()) {
			fillBlogUserSettingsFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	protected void fillBlogUserSettingsFromResultSet(BlogUserSettings s, ResultSet rs) throws Exception{
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setID(rs.getInt(("ID")));
		s.setTITLE(rs.getString("TITLE"));
		s.setSTYLE(rs.getInt("THEMES_ID"));
		s.setMAXNUMBERPOST(rs.getInt("MAX_NUMBER_POSTS"));
		s.setBLOCKCONTENT1(rs.getString("BLOCK_CONTENT_1"));
		s.setBLOCKTITLE1(rs.getString("BLOCK_TITLE_1"));
		s.setBLOCKCONTENT2(rs.getString("BLOCK_CONTENT_2"));
		s.setBLOCKTITLE2(rs.getString("BLOCK_TITLE_2"));
		s.setBLOCKCONTENT3(rs.getString("BLOCK_CONTENT_3"));
		s.setBLOCKTITLE3(rs.getString("BLOCK_TITLE_3"));
		s.setBLOCKCONTENT4(rs.getString("BLOCK_CONTENT_4"));
		s.setBLOCKTITLE4(rs.getString("BLOCK_TITLE_4"));
		s.setBLOCKCONTENT5(rs.getString("BLOCK_CONTENT_5"));
		s.setBLOCKTITLE5(rs.getString("BLOCK_TITLE_5"));
	}
	
	public void update(BlogUserSettings bs) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogUserSettingsModel.update"));
		p.setString(1,bs.getDESCRIPTION());
		p.setInt(2,bs.getMAXNUMBERPOST());
		p.setInt(3,bs.getSTYLE());
		p.setString(4,bs.getTITLE());
		p.setString(5,bs.getBLOCKTITLE1());
		p.setString(6,bs.getBLOCKCONTENT1());
		p.setString(7,bs.getBLOCKTITLE2());
		p.setString(8,bs.getBLOCKCONTENT2());
		p.setString(9,bs.getBLOCKTITLE3());
		p.setString(10,bs.getBLOCKCONTENT3());
		p.setString(11,bs.getBLOCKTITLE4());
		p.setString(12,bs.getBLOCKCONTENT4());
		p.setString(13,bs.getBLOCKTITLE5());
		p.setString(14,bs.getBLOCKCONTENT5());
		p.setInt(15,bs.getID());
		p.executeUpdate();
		p.close();
	}

	public void insert(BlogUserSettings bs) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogUserSettingsModel.insert"));
		p.setString(1,bs.getDESCRIPTION());
		p.setInt(2,bs.getMAXNUMBERPOST());
		p.setInt(3,bs.getSTYLE());
		p.setString(4,bs.getTITLE());
		p.setInt(5,bs.getID());
		p.executeUpdate();
		p.close();

	}

}
